import { Http, Headers, Response,Jsonp, RequestOptions, URLSearchParams } from '@angular/http';
import 'rxjs/add/operator/toPromise';

import { Injectable } from '@angular/core';

import { Driver } from './driver';
import { SERVER} from '../../app.config';

@Injectable()
export class DriverService {
    constructor(private http: Http){

    }

    getDrivers(): Promise<Driver[]> {
        return this.http.get(SERVER.DRIVER)
        .toPromise()
        .then(response => response.json() as Driver[])
        .catch(this.handleError);
    }

    getDriversByCarrierID(carrier_id: string): Promise<Driver[]>{
      let headers = new Headers({
        'Content-type': 'application/json'});
      let url = `${SERVER.DRIVER}`+'?carrier_id='+ carrier_id;
      return this.http.get(url,{ headers: headers})
        .toPromise()
        .then(response => response.json() as Driver[])
        .catch(this.handleError);

    }


    getDriver(driver_id: number) {
        return this.getDrivers()
            .then(drivers => drivers.find(driver => driver.driver_id ===driver_id ));
    }

    private handleError(error: any): Promise<any> {
        console.error('错误：', error);

        return Promise.reject(error.message || error);
    }

    //添加
    private post(driver: Driver): Promise<Driver> {
        let headers = new Headers({
            'Content-type': 'application/json'});

        return this.http.post(SERVER.DRIVER, JSON.stringify(driver), {headers: headers})
        .toPromise()
        // .then(res => res.json().data)
        .then(() => driver)
        .catch(this.handleError);
    }

    //更新
    private put(driver: Driver): Promise<Driver> {
        let headers = new Headers();
        headers.append('Content-Type', 'application/json');

        let url = `${SERVER.DRIVER}` + '/' + driver.driver_id;

        return this.http.put(url, JSON.stringify(driver), {headers: headers})
                   .toPromise()
                   .then(() => driver)
                   .catch(this.handleError);
    }

    //删除
    delete(driver: Driver): Promise<Response> {
        let headers = new Headers();
        headers.append('Content-Type', 'application/json');

        let url = `${SERVER.DRIVER}/${driver.driver_id}`;

        return this.http
                   .delete(url, { headers: headers})
                   .toPromise()
                   .catch(this.handleError);
    }

    //保存
    save(driver: Driver): Promise<Driver> {
        if (driver.driver_id) {
            return this.put(driver)
        }
        return this.post(driver);
    }

    //重置密码
    resetPassword(driver: Driver): Promise<Driver> {
        let headers = new Headers();
        headers.append('Content-Type', 'application/json');

        let url = `${SERVER.DRIVER}` + '/resetpassword';

        return this.http
                   .post(url, JSON.stringify(driver), { headers: headers})
                   .toPromise()
                   .then(res => res.json())
                   .catch(this.handleError);
    }
}
